import datetime
import os

from cc.appcoin2.support.order.py_trade_client import PyTradeClient
from coin.base.datetime_util import to_timestamp_int
from coin.proto.coin_query_pb2 import CurrencyTransferElement
from coin2.exchange.order.system_pb2 import OrderSystemConfig
from google.protobuf.json_format import ParseDict


class PyPrivateClient(object):
  def __init__(self, os_config):
    assert isinstance(os_config, OrderSystemConfig)
    self._client = PyTradeClient(os_config.SerializeToString())

  def query_transfer_histories(self, start_ts, end_ts):
    results = []
    raw_data = self._client.query_transfer_histories(start_ts, end_ts)
    for elem in raw_data:
      result = CurrencyTransferElement()
      result.ParseFromString(elem)
      results.append(result)
    return results

if __name__ == "__main__":
  start_time = datetime.datetime(2022, 7, 1)
  end_time = datetime.datetime(2022, 10, 1)
  key_filepath = '../../coin_key/view/prestoinv0/binance/view_key.json'
  os_config = {
      'mea': 'Spot.Binance.v1',
      'products': {
        'norms': [
          'BTC-USDT'
        ]
      },
      'connection_config': {
          'key_filepath': os.path.abspath(key_filepath)
      }
  }
  os_config_proto = OrderSystemConfig()
  ParseDict(os_config, os_config_proto)
  client = PyPrivateClient(os_config_proto)
  data = client.query_transfer_histories(
      to_timestamp_int(start_time), to_timestamp_int(end_time))
  print(data)
